
// Copyright (C) 2007 Richard Uhler
// This file is part of Esiply.
//
// Esiply is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "Array.h"
#include "ArrayExpression.h"
#include "Number.h"
#include "Object.h"
#include "String.h"
#include "Value.h"

ArrayExpression::ArrayExpression()
{}

ArrayExpression::ArrayExpression(Expression* exp)
{
    push_back(exp);
}

void ArrayExpression::push_back(Expression* exp)
{
    m_exps.push_back(exp);
}

void ArrayExpression::append(ArrayExpression* other)
{
    m_exps.insert(m_exps.end(), other->m_exps.begin(), other->m_exps.end());
}

Value* ArrayExpression::evaluate(Scope* scope)
{
    // Create a new Array, and add elements in order. If an expression
    // is Null, don't create an element at that index.
    Array* array = new Array();
    array->put(new String("length"), new Number(m_exps.size()), PA_EMPTY);
    for (unsigned int i = 0; i < m_exps.size(); i++)
    {
        if (m_exps[i] != NULL)
        {
            String* index = (new Number(i))->toString();
            array->put(index, m_exps[i]->evaluate(scope)->getValue(), PA_EMPTY);
        }
    }
    return array;
}
            
